package expressions.slogo;

import parsing.Separates;
import parsing.Token;
import exceptions.AssignmentException;
import expressions.Expression;
import expressions.IContext;
import expressions.ParentExpression;

@Token(regex = "=")
@Separates
public class Assignment extends ParentExpression<Double>
{

    public Assignment()
    {
        super(2);
    }

    @Override
    public Double evaluate(IContext<Double> context)
    {
        Expression<Double> left = children.get(0);
        Expression<Double> right = children.get(1);

        if (left instanceof Variable)
        {
            String name = ((Variable) left).getVarName();
            context.setVariable(name, right);

            return right.evaluate(context);
        }
        else
            throw new AssignmentException(left);
    }

}
